﻿/**
 * Created by Administrator on 2017/6/17.
 */
import Vue from 'vue'
import VueX from 'vuex'
import * as actions from './actions'
import * as getters from './getters'

import JSON from "JSON";
//var JSON = require('circular-json');
import woutlaCrypto from '@/crypto';
// import axios from '../axios/index.js';
import {AlertModule, AlertPlugin, Toast, ToastPlugin} from 'vux'

Vue.use(VueX)
Vue.use(AlertPlugin)
Vue.use(ToastPlugin)

const state = {
  default_account: {},
  wallet: false,
  config: false,
  rpc_url : "",
}
const mutations = {
  reset_state(state) {
    state.wallet = false;
    state.default_account = {};
  },
  updateConfig(state, config) {
    state.config = config;
  },
  updateRpcUrl(state, url){
    state.rpc_url = url;
  },
  changeWallet(state, address) {
    if (address)
      address = address.toLowerCase();
    if (!state.wallet) {
      this.loadWallet(state);
    }
    var default_account = state.wallet[address];
    if (default_account) {
      state.default_account = default_account;
    }
  },
  addWallet(state, account) {
    // 可能会有多用户安全问题
    console.log("11111111");
    account.address = account.address.toLowerCase();
    if (!state.wallet) {
      this.loadWallet(state);
    }
    state.wallet[account.address] = account;
    console.log("522222222", state.wallet);
    var encStr = JSON.stringify(state.wallet);
    encStr = woutlaCrypto.encrypt(encStr);
    localStorage.localWallet = encStr;
    console.log("addwallet:6666666666", state.wallet)
    state.default_account = account;
  },
  updateWallet(state, account) {
    account.address = account.address.toLowerCase();
    if (!state.wallet) {
      this.loadWallet(state);
    }
    state.wallet[account.address] = account;
    var encStr = JSON.stringify(state.wallet);
    encStr = woutlaCrypto.encrypt(encStr);
    localStorage.localWallet = encStr;
  },
  deleteAccount(state, address) {

    // address = address.toLowerCase();
    // if (!state.wallet) {
    //   this.loadWallet(state);
    // }
    //
    // var encStr = JSON.stringify(state.wallet);
    // encStr = woutlaCrypto.encrypt(encStr);
    // localStorage.localWallet = encStr;
    delete state.wallet[address];
  },
  loadWallet(state) {
    if (!state.wallet) {
      state.wallet = {};
      var wallets_config = localStorage.localWallet;
      if (wallets_config) {
        try {
          wallets_config = woutlaCrypto.decrypt(wallets_config);
          state.wallet = JSON.parse(wallets_config);
        } catch (e) {
          console.log(e);
        }
      }
    }
  }
  // loadWallets(state,callback){
  //   var default_address = localStorage.default_address;
  //   var default_account;
  //   axios.http.post('/api/app.default_account/wallet',{}).then(res => {
  //
  //     if(res.data.wallet!=""){
  //       state.wallet = res.data.wallet;
  //       default_account = state.wallet[default_address];
  //       if(!default_account)
  //       {
  //          for(default_address in state.wallet)
  //          {
  //            default_account = state.wallet[default_address];
  //            localStorage.default_address = default_address;
  //            break;
  //          }
  //       }
  //       state.default_account = {name: default_account.name, address: default_account.address, enc_privateKey: default_account.enc_privateKey};
  //       state.balance_loading = false;
  //     }
  //     if(callback)
  //       callback.call(null);
  //   }).catch(err => {
  //     state.balance_loading = false;
  //     console.log("err");
  //     console.log(err);
  //   });
  // },
  // loadProperties(state,callback){
  //    // console.log(Vue);
  //     state.balance_loading = true;
  //     axios.http.post('/api/app.default_account/property/sum',{address:state.default_account.address}).then(res => {
  //       state.properties = res.data.properties;
  //     state.balance_loading = false;
  //     state.properties_amount = 0;
  //     for(var i in state.properties)
  //     {
  //       state.properties_set[state.properties[i].type] = state.properties[i];
  //       if(state.properties[i].amount>0)
  //         state.properties_amount += state.properties[i].amount*state.properties[i].price;
  //     }
  //     if(callback)
  //       callback.call(null);
  //   }).catch(err => {
  //       state.balance_loading = false;
  //       //if(err)
  //       //   Vue.$vux.toast.text("网络超时，下拉重试");
  //       if(callback)
  //           callback.call(null);
  //       console.log("err");
  //       console.log(err);
  //   });
  // },
  // refresh_coin_type(state){
  //   var id = parseInt(state.coin_info.coin_type.id);
  //   axios.http.post('/api/app.user/account/asset',{coin_type:id,single:1}).then(res => {
  //     if(res.errcode==0){
  //       state.coin_info= res.data.asset;
  //     }
  //   })
  // },
}
//创建store 实例
export default new VueX.Store({
  actions,
  getters,
  state,
  mutations
})
